package com.evan.aiu.controller;

import com.evan.aiu.entity.Answer;
import com.evan.aiu.result.Result;
import com.evan.aiu.result.ResultFactory;
import com.evan.aiu.service.AnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Api("回答管理")
public class AnswerController {
    @Autowired
    AnswerService answerService;

    @ApiOperation("新建回答")
    @PostMapping("api/admin/answer")
    public Result saveAnswer(@RequestBody @Valid Answer answer) {
        answerService.addOrUpdate(answer);
        return ResultFactory.buildSuccessResult("保存成功");
    }

    @ApiOperation("获取回答列表")
    @GetMapping("/api/answer/{size}/{page}")
    public Result listAnswer(@PathVariable("size") int size, @PathVariable("page") int page) {
        return ResultFactory.buildSuccessResult(answerService.list(page - 1, size));
    }

    @ApiOperation("通过id获取回答")
    @GetMapping("/api/answer/{id}")
    public Result getOneAnswer(@PathVariable("id") int id) {
        return ResultFactory.buildSuccessResult(answerService.findById(id));
    }

    @ApiOperation("通过aid获取回答")
    @GetMapping("/api/answerByAid/{aid}")
    public Result getAnswer(@PathVariable("aid") int aid) {
        return ResultFactory.buildSuccessResult(answerService.findByAid(aid));
    }

    @ApiOperation("通过id删除回答")
    @DeleteMapping("/api/admin/answer/{id}")
    public Result deleteArticle(@PathVariable("id") int id) {
        answerService.delete(id);
        return ResultFactory.buildSuccessResult("删除成功");
    }
}
